package com.many.backs.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.many.common.constants.NosqlKeyManager;
import com.many.common.exception.ErrorCodes;
import com.many.data.entity.SysUser;
import com.many.data.entity.SysUserLogin;
import com.many.data.mapper.SysUserMapper;
import com.many.backs.service.sys.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-06-30
 */
@Service
 public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    private Long cumulativeVisits = 1024L;
    @Override
    public boolean updates(SysUser sysUser) {
        int i = baseMapper.updateById(sysUser);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public R login(SysUser sysUser) {
        if (ObjectUtils.isEmpty(sysUser)){
            return R.failed(ErrorCodes.PASSWORD_WRONG);
        }
        if (ObjectUtils.isEmpty(sysUser.getUserName()) || ObjectUtils.isEmpty(sysUser.getPassword())){
            return R.failed(ErrorCodes.PASSWORD_WRONG);
        }
        SysUser user = baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, sysUser.getUserName()).eq(SysUser::getPassword, sysUser.getPassword()).eq(SysUser::getDeleteFlag,0));
        if (ObjectUtils.isEmpty(user)){
            return R.failed(ErrorCodes.PASSWORD_WRONG);
        }
        SysUserLogin userLogin=new SysUserLogin();
        String token = UUID.randomUUID().toString();
        userLogin.setToken(token);
        BeanUtils.copyProperties(user,userLogin);

        //如果有用户重新登录，之前的token清除
        if(redisTemplate.hasKey(NosqlKeyManager.SYS_USER_ID_TOKEN + "::" + userLogin.getUserId())){
            String beforeToken = (String) redisTemplate.opsForValue().get(NosqlKeyManager.SYS_USER_ID_TOKEN + "::" + userLogin.getUserId());
            redisTemplate.delete(NosqlKeyManager.SYS_USER_TOKEN + "::" + beforeToken);
        }
        redisTemplate.opsForValue().set(NosqlKeyManager.SYS_USER_TOKEN + "::" + token, userLogin, 1, TimeUnit.DAYS);
        redisTemplate.opsForValue().set(NosqlKeyManager.SYS_USER_ID_TOKEN + "::" + userLogin.getUserId(), token, 1, TimeUnit.DAYS);
        return R.ok(userLogin);
    }

    @Override
    public R redis() {
        synchronized (this) {
            ++cumulativeVisits;
            redisTemplate.opsForValue().set(NosqlKeyManager.USER_REDIS,cumulativeVisits);
        }
        Long temp = cumulativeVisits;
        if (redisTemplate.hasKey(NosqlKeyManager.USER_REDIS)){
            temp = (Long) redisTemplate.opsForValue().get(NosqlKeyManager.USER_REDIS);
        }
        return R.ok(temp);
    }

    @Override
    public R redisList() {
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        List<SysUser> sysUsers = baseMapper.selectList(null);
        redisTemplate.opsForValue().set("list",sysUsers);
        List<SysUser> list1=new ArrayList<>();
        list1 = (List<SysUser>) redisTemplate.opsForValue().get("list");
        System.out.println(list1);
        return R.ok(list1);
    }

    public static void main(String[] args) {
        String a="abcd123";
        System.out.println(a.indexOf("d"));
        System.out.println(a.charAt(3));
        StringBuilder stringBuilder=new StringBuilder(a);
        String s = stringBuilder.reverse().toString();
        System.out.println(s);
    }


}
